package cn.fenqing168.springcloud.controller;

import cn.fenqing168.springcloud.service.feign.PaymentHystrixFiegnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 */
@RestController
@Slf4j
//@DefaultProperties(defaultFallback = "paymentGlobalFallbackMethod")
public class OrderHystrixController {

    @Autowired
    private PaymentHystrixFiegnService paymentHystrixFiegnService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfoOk(@PathVariable("id") Long id){
        String res = paymentHystrixFiegnService.paymentInfoOk(id);
        log.info("result:{}", res);
        return res;
    }

    /**
     * 超时的接口
     * @param id
     * @return
     */
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    //先注释指定的fallback，使其触发默认的fallback
//    @HystrixCommand(
//            fallbackMethod = "paymentInfoTimeoutFallBack",
//            commandProperties = {
//                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
//            }
//    )
    //@HystrixCommand
    public String paymentInfoTimeout(@PathVariable("id") Long id){
        String res = paymentHystrixFiegnService.paymentInfoTimeout(id);
        log.info("result:{}", res);
        return res;
    }

    /**
     * paymentInfoTimeout 方法的fallback方法
     * @param id
     * @return
     */
    public String paymentInfoTimeoutFallBack(Long id){
        return "我是消费者80，对方支付系统繁忙，请稍后再试";
    }

    /**
     * 全局默认的fallback方法
     * @return
     */
    public String paymentGlobalFallbackMethod(){
        return "触发全局的fallback";
    }

}
